<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库区id" prop="zoneId">
        <el-input
          v-model="queryParams.zoneId"
          placeholder="请输入库区id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种类id" prop="rawProductId">
        <el-input
          v-model="queryParams.rawProductId"
          placeholder="请输入种类id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存名称" prop="inventoryName">
        <el-input
          v-model="queryParams.inventoryName"
          placeholder="请输入库存名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存数量" prop="inventoryNum">
        <el-input
          v-model="queryParams.inventoryNum"
          placeholder="请输入库存数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入库存重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品规格" prop="goodSpecification">
        <el-input
          v-model="queryParams.goodSpecification"
          placeholder="请输入物品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['storage:warehouseInventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storage:warehouseInventory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storage:warehouseInventory:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storage:warehouseInventory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseInventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存id" align="center" prop="batchId" />
      <el-table-column label="库区id" align="center" prop="zoneId" />
      <el-table-column label="种类id" align="center" prop="rawProductId" />
      <el-table-column label="库存名称" align="center" prop="inventoryName" />
      <el-table-column label="库存数量" align="center" prop="inventoryNum" />
      <el-table-column label="总重量" align="center" prop="weight" />
      <el-table-column label="重量单位" align="center" prop="weightUnit" />
      <el-table-column label="总体积" align="center" prop="volume" />
      <el-table-column label="体积单位" align="center" prop="volumeUnit" />
      <el-table-column label="库存类型" align="center" prop="inventoryType" />
      <el-table-column label="物品规格" align="center" prop="goodSpecification" />
      <el-table-column label="货源地" align="center" prop="origin" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['storage:warehouseInventory:detail']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:warehouseInventory:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storage:warehouseInventory:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情弹窗 -->
    <el-dialog title="查看详情" :visible.sync="detailOpen" width="750px" append-to-body>
      <el-table :data="detailData" border style="width: 100%">
        <el-table-column prop="itemId" label="物品编号" width="100"></el-table-column>
        <el-table-column prop="warehouseId" label="仓库id" width="100"></el-table-column>
        <el-table-column prop="batchId" label="批次id" width="100"></el-table-column>
        <el-table-column prop="location" label="库位id" width="100"></el-table-column>
        <el-table-column prop="areaId" label="库区id" width="100"></el-table-column>
        <el-table-column prop="weight" label="单件重量" width="100"></el-table-column>
        <el-table-column prop="volume" label="单件体积" width="100"></el-table-column>


      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改库存查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库区id" prop="zoneId">
          <el-input v-model="form.zoneId" placeholder="请输入库区id" />
        </el-form-item>
        <el-form-item label="种类id" prop="rawProductId">
          <el-input v-model="form.rawProductId" placeholder="请输入种类id" />
        </el-form-item>
        <el-form-item label="库存名称" prop="inventoryName">
          <el-input v-model="form.inventoryName" placeholder="请输入库存名称" />
        </el-form-item>
        <el-form-item label="库存数量" prop="inventoryNum">
          <el-input v-model="form.inventoryNum" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="总重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入库存重量" />
        </el-form-item>
        <el-form-item label="重量单位" prop="weightUnit">
          <el-input v-model="form.weightUnit" placeholder="请输入重量单位" />
        </el-form-item>
        <el-form-item label="总体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入单件体积" />
        </el-form-item>
        <el-form-item label="体积单位" prop="volumeUnit">
          <el-input v-model="form.volumeUnit" placeholder="请输入体积单位" />
        </el-form-item>
        <el-form-item label="物品规格" prop="goodSpecification">
          <el-input v-model="form.goodSpecification" placeholder="请输入物品规格" />
        </el-form-item>
        <el-divider content-position="center">库存详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddItemDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteItemDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="itemDetailList" :row-class-name="rowItemDetailIndex" @selection-change="handleItemDetailSelectionChange" ref="itemDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="库区id" prop="areaId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.areaId" placeholder="请输入库区id" />
            </template>
          </el-table-column>
          <el-table-column label="库位id" prop="location" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.location" placeholder="请输入库位id" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWarehouseInventory,
  getWarehouseInventory,
  delWarehouseInventory,
  addWarehouseInventory,
  updateWarehouseInventory,
  getWarehouseInventoryDetail
} from "@/api/storage/warehouseInventory";

export default {
  name: "WarehouseInventory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedItemDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存查询表格数据
      warehouseInventoryList: [],
      // 库存详情表格数据
      itemDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        zoneId: null,
        rawProductId: null,
        inventoryName: null,
        inventoryNum: null,
        weight: null,
        inventoryType: null,
        goodSpecification: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseId: [
          { required: true, message: "仓库id不能为空", trigger: "blur" }
        ],
        zoneId: [
          { required: true, message: "库区id不能为空", trigger: "blur" }
        ],
        inventoryName: [
          { required: true, message: "库存名称不能为空", trigger: "blur" }
        ],
        inventoryType: [
          { required: true, message: "库存类型不能为空", trigger: "change" }
        ],
      },
      // 详情弹窗显示控制
      detailOpen: false,
      // 详情数据
      detailData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存查询列表 */
    getList() {
      this.loading = true;
      listWarehouseInventory(this.queryParams).then(response => {
        this.warehouseInventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        warehouseId: null,
        zoneId: null,
        rawProductId: null,
        inventoryName: null,
        inventoryNum: null,
        weight: null,
        weightUnit: null,
        volume: null,
        volumeUnit: null,
        inventoryType: null,
        goodSpecification: null
      };
      this.itemDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.batchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batchId = row.batchId || this.ids
      getWarehouseInventory(batchId).then(response => {
        this.form = response.data;
        this.itemDetailList = response.data.itemDetailList;
        this.open = true;
        this.title = "修改库存查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.itemDetailList = this.itemDetailList;
          if (this.form.batchId != null) {
            updateWarehouseInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouseInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batchIds = row.batchId || this.ids;
      this.$modal.confirm('是否确认删除库存查询编号为"' + batchIds + '"的数据项？').then(function() {
        return delWarehouseInventory(batchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 库存详情序号 */
    rowItemDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 库存详情添加按钮操作 */
    handleAddItemDetail() {
      let obj = {};
      obj.areaId = "";
      obj.location = "";
      this.itemDetailList.push(obj);
    },
    /** 库存详情删除按钮操作 */
    handleDeleteItemDetail() {
      if (this.checkedItemDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的库存详情数据");
      } else {
        const itemDetailList = this.itemDetailList;
        const checkedItemDetail = this.checkedItemDetail;
        this.itemDetailList = itemDetailList.filter(function(item) {
          return checkedItemDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleItemDetailSelectionChange(selection) {
      this.checkedItemDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/warehouseInventory/export', {
        ...this.queryParams
      }, `warehouseInventory_${new Date().getTime()}.xlsx`)
    },
    /** 查看详情按钮操作 */
    handleView(row) {
      const batchId = row.batchId;
      getWarehouseInventoryDetail(batchId).then(response => {
        this.detailData = response;
        this.detailOpen = true;
      }).catch(error => {
        console.error("获取详情数据失败", error);
        this.$modal.msgError("获取详情数据失败");
      });
    }
  }
};
</script>
